Page({
    data: {
        textData: '订单详情',
        backgroundData: 'transparent',
        secureTop: 0, //顶部距离初始值
        active: '',
        state: '',
        detailObj: {},
        cancelText: '', //取消文本输入原因
        payLoading: false, //确认支付Loading
        showJixu: false,
        cancelShow: false, //取消订单弹出层
        okCancelBtnLoading: false, //确认取消的loading
        submitTakeLoading: false, //确认收货loading
        sh_id: '',
    },

    onLoad(options) {
        const active = options.active;
        if (options) {
            const state = options.state;
            const id = options.id;
            this.setData({
                state,
            });
            // console.log(options);
            this.setData({
                sh_id: options.pay_wx_id,
            });
            this.getDetail(id);
        }
        const computedTop = wx.getSystemInfoSync().statusBarHeight;
        this.setData({
            active,
            secureTop: computedTop,
        });
        if (active == 1) {
            this.setData({
                textData: '待支付',
            });
        }
        if (active == 2) {
            this.setData({
                textData: '待发货',
            });
        }
        if (active == 3) {
            this.setData({
                textData: '待收货',
            });
        }
        if (active == 4) {
            this.setData({
                textData: '已完成',
            });
        }
        if (active == 5) {
            this.setData({
                textData: '已取消',
            });
        }
    },

    // 订单详情
    getDetail(id) {
        this.request({
            url: 'user/my/order/detail',
            data: {
                id,
            },
            loadingTip: '加载中',
        })
            .then(res => {
                this.setData({
                    detailObj: res.data,
                });
            })
            .catch(err => {});
    },

    onInputCancel(e) {
        this.setData({
            cancelText: e.detail.value,
        });
    },
    // 打开继续支付弹出层
    clickPay() {
        this.setData({
            showJixu: true,
        });
    },

    onVisibleChange() {
        this.setData({
            showJixu: false,
        });
    },

    // 取消订单弹出
    onCancelOrder() {
        this.setData({
            cancelShow: true,
        });
    },

    // 取消订单提交
    onSubmitCancael() {
        if (!this.data.cancelText)
            return wx.showToast({
                title: '请填写取消原因',
                icon: 'none',
            });
        this.setData({
            okCancelBtnLoading: true,
        });
        this.request({
            url: 'user/my/order/cancel',
            method: 'POST',
            data: {
                id: this.data.detailObj.id,
                cancel_cause: this.data.cancelText,
            },
        })
            .then(res => {
                this.setData({
                    okCancelBtnLoading: false,
                });
                // 提示
                wx.showToast({
                    title: res.msg,
                });
                // 返回上一页
                wx.navigateBack();
            })
            .catch(err => {
                this.setData({
                    okCancelBtnLoading: false,
                });
            });
    },

    onClosePopup() {
        this.setData({
            cancelShow: false,
        });
    },

    // 确认支付提交(继续支付)
    okPay() {
        this.setData({
            payLoading: true,
        });
        this.request({
            url: 'user/my/order/continuePay',
            method: 'POST',
            data: {
                id: this.data.detailObj.id,
            },
        })
            .then(res => {
                if (res.data.pay_data == null)
                    return wx.showToast({
                        title: '该订单仅支持线下支付',
                        icon: 'none',
                    });

                const { timeStamp, paySign, signType, package: packageId, nonceStr } = res.data.pay_data;
                wx.requestPayment({
                    timeStamp: String(timeStamp),
                    nonceStr,
                    package: packageId,
                    signType,
                    paySign,
                    success: async res => {
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                        });
                        await this.sleep(1000);
                        wx.navigateBack();
                    },
                    fail: async err => {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none',
                        });
                        await this.sleep(1000);
                        wx.navigateBack();
                    },
                });
                this.setData({
                    payLoading: false,
                });
            })
            .catch(err => {
                this.setData({
                    payLoading: false,
                });
            });
    },
    // 确认收货按钮
    submitTake() {
        const extraData = {
            transaction_id: this.data.sh_id, // 商户系统的订单号++
        };

        wx.openBusinessView({
            businessType: 'weappOrderConfirm',
            extraData,
            success(res) {
                this.setData({       
                    submitTakeLoading: true,
                });
                this.request({
                    url: 'user/my/order/confirmReceive',
                    method: 'POST',
                    data: {
                        id: this.data.detailObj.id,
                    },
                })
                    .then(res => {
                        this.setData({
                            submitTakeLoading: false,
                        });
                        // 提示
                        wx.showToast({
                            title: res.msg,
                        });
                        // 返回上一页
                        wx.navigateBack();
                    })
                    .catch(err => {
                        this.setData({
                            submitTakeLoading: false,
                        });
                    });
            },
            fail(err) {},
        });
    },

    onPageScroll(e) {
        if (e.scrollTop > 1) {
            this.setData({
                backgroundData: '#FCBEAC',
            });
        } else {
            this.setData({
                backgroundData: 'transparent',
            });
        }
    },
});
